/*
 * @Author: LiuYan 15136056318@163.com
 * @Date: 2023-11-23 10:52:27
 * @LastEditors: LiuYan 15136056318@163.com
 * @LastEditTime: 2023-11-29 13:43:43
 * @FilePath: \ysys_borad\src\utils\index.ts
 * @Description:
 */
/**
 * @description: px转vw
 * @param {number} px
 * @return {*}
 */
export const pxToVw = (px: number): string => {
  return (px / 1920) * 100 + 'vw'
}
/**
 * @description: px转vh
 * @param {number} px
 * @return {*}
 */
export const pxToVh = (px: number): string => {
  return (px / 1080) * 100 + 'vh'
}
/**
 * @description: 16进制转换为RGB
 * @param {*} str
 * @param {*} alpha  透明度
 * @return {*}
 */
export const _16ToRgb = (str: string, alpha = 1) => {
  const result: any = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(str)
  return `rgba(${parseInt(result[1], 16)},${parseInt(result[2], 16)},${parseInt(
    result[3],
    16
  )},${alpha})`
}

/**
 * @description: RGB转换为16进制
 * @param {*} str
 * @return {*}
 */
export const _rgbTo16 = (str: string) => {
  const values: any = str
    .replace(/rgba?\(/, '')
    .replace(/\)/, '')
    .replace(/[\s+]/g, '')
    .split(',')
  const a = parseFloat(values[3] || 1)
  const r = Math.floor(a * parseInt(values[0]) + (1 - a) * 255)
  const g = Math.floor(a * parseInt(values[1]) + (1 - a) * 255)
  const b = Math.floor(a * parseInt(values[2]) + (1 - a) * 255)
  return (
    '#' +
    ('0' + r.toString(16)).slice(-2) +
    ('0' + g.toString(16)).slice(-2) +
    ('0' + b.toString(16)).slice(-2)
  )
}
